﻿using System;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Players.Manager
{
    public partial class Manager
    {

        private void HandleKeyPressWalkState(Input input)
        {
            switch (input)
            {
                case Input.Attack:
                    ChangeState(PlayerState.Attack);
                    break;
            }
        }

        private void HandleKeyReleaseWalkState(Input input) {}

        private void InitializeWalkState()
        {
            if(_input.UpKeyState)
                ChangeCurrentDirection(Direction.Up);
            else if(_input.DownKeyState)
                ChangeCurrentDirection(Direction.Down);
            else if(_input.LeftKeyState)
                ChangeCurrentDirection(Direction.Left);
            else if(_input.RightKeyState)
                ChangeCurrentDirection(Direction.Right);
        }

        void ChangeCurrentDirection(Direction direction)
        {
            _latestDirection = direction;
            _currentDirection = direction;
            if(_isCarrying)
                SetWalkCarryModel(direction);
            else
                SetWalkModel(direction);
        }

        private void UpdateWalkState(TimeSpan gameTime)
        {
            TE_Vector2 speedVector = new TE_Vector2((float)(WalkSpeed * Game.Options.GridSize * gameTime.TotalSeconds),
                                                    (float)(WalkSpeed * Game.Options.GridSize * gameTime.TotalSeconds));

            if (_input.UpKeyState)
            {
                HandleMovement(new TE_Vector2(0, -speedVector.Y));

                if (_currentDirection != Direction.Up)
                    ChangeCurrentDirection(Direction.Up);
                
            }
            else if (_input.DownKeyState)
            {
                HandleMovement(new TE_Vector2(0, speedVector.Y));

                if (_currentDirection != Direction.Down)
                    ChangeCurrentDirection(Direction.Down);
                
            }
            else if (_input.LeftKeyState)
            {
                HandleMovement(new TE_Vector2(-speedVector.X, 0));

                if (_currentDirection != Direction.Left)
                    ChangeCurrentDirection(Direction.Left);
            }
            else if (_input.RightKeyState)
            {
                HandleMovement(new TE_Vector2(speedVector.X, 0));

                if (_currentDirection != Direction.Right)
                    ChangeCurrentDirection(Direction.Right);
            }
            else
            {
                ChangeState(PlayerState.Idle);
            }

            //If the game ends while the player is walking, revert back to idle
            if(!Playscreen.Controller.IsStarted)
            {
                ChangeState(PlayerState.Idle);
            }
        }
    }
}
